//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.未分类.demo1AND20;

public class demo14 {
    public demo14() {
    }

    public static void main(String[] args) {
        String[] strs = new String[]{"flower", "fkow"};
        System.out.println(longestCommonPrefix(strs));
    }

    public static String longestCommonPrefix(String[] strs) {
        int i = 1;
        int j = 0;
        String str = strs[0];

        while(i < strs.length) {
            if (strs[i].length() < str.length()) {
                for(j = strs[i].length(); j >= 0; --j) {
                    if (strs[i].substring(0, j).equals(str.substring(0, j))) {
                        str = strs[i].substring(0, j);
                        break;
                    }
                }

                if (j < 0) {
                    return "";
                }

                ++i;
            } else if (strs[i].length() > str.length()) {
                for(j = str.length(); j >= 0; --j) {
                    if (str.substring(0, j).equals(strs[i].substring(0, j))) {
                        str = strs[i].substring(0, j);
                        break;
                    }
                }

                if (j < 0) {
                    return "";
                }

                ++i;
            } else {
                for(j = str.length(); j >= 0; --j) {
                    if (str.substring(0, j).equals(strs[i].substring(0, j))) {
                        str = strs[i].substring(0, j);
                        break;
                    }
                }

                if (j < 0) {
                    return "";
                }

                ++i;
            }
        }

        return str;
    }
}
